<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head th:replace="admin/_fragments :: head(~{::title})">
    <title>shell845 | blog</title>
</head>

<body class="m-sticky-site">
<!-- Nav bar-->
<nav th:replace="admin/_fragments :: menu(1)"></nav>
<div th:fragment="subbar" class="ui attached inverted pointing menu m-opacity-small">
    <div class="ui container">
        <div class="right menu">
            <a href="#" th:href="@{/admin/blogs/input}" class="olive item">Add</a>
            <a href="#" th:href="@{/admin/blogs}" class="active olive item">List</a>
        </div>
    </div>
</div>

<!-- Content -->
<div class="m-container-small m-padded-tb-mid">
    <div class="ui container">
        <!--notification-->
        <div class="ui success message" th:unless="${#strings.isEmpty(message)}">
            <i class="close icon"></i>
            <div class="header">Notification:</div>
            <p th:text="${message}">Operation success</p>
        </div>
        <div class="ui segment form">
            <input type="hidden" name="page">
            <div class="inline fields">
                <div class="field">
                    <input type="text" name="title" placeholder="Title">
                </div>
                <div class="field">
                    <div class="ui selection dropdown">
                        <input type="hidden" name="type.id">
                        <i class="dropdown icon"></i>
                        <div class="default text">Type</div>
                        <div class="menu">
                            <div th:each="type : ${types}" class="item" data-value="1" th:data-value="${type.id}" th:text="${type.name}">Error</div>
                            <!--/*-->
                            <div class="item" data-value="2">Log</div>
                            <!--*/-->
                        </div>
                    </div>
                </div>
                <div class="field">
                    <div class="ui checkbox">
                        <input type="checkbox" id="recommend" name="recommend">
                        <label for="recommend">Recommend</label>
                    </div>
                </div>
                <div class="field">
                    <button type="button" id="search-btn" class="ui olive basic button"><i class="search icon"></i>Search</button>
                </div>
            </div>
        </div>
        <!--table-->
        <div id="table-container">
            <table class="ui table" th:fragment="blogList">
                <thead>
                <tr>
                    <th></th>
                    <th>Title</th>
                    <th>Type</th>
                    <th>Update time</th>
                    <th>Published</th>
                    <th>Recommend</th>
                    <th class="right aligned">Operation</th>
                </tr>
                </thead>
                <tbody>
                <tr th:each="blog, iterStat : ${page.content}">
                    <td th:text="${iterStat.count}">1</td>
                    <td th:text="${blog.title}">title</td>
                    <td th:text="${blog.type.name}">types</td>
                    <td th:text="${blog.updateTime}">2020-02-02</td>
                    <td th:text="${blog.published? 'Yes' : 'No'}">Published</td>
                    <td th:text="${blog.recommend? 'Yes' : 'No'}">Yes</td>
                    <td class="right aligned">
                        <a href="#" th:href="@{/admin/blogs/{id}/input(id=${blog.id})}" class="ui mini olive button"> Edit </a>
                        <a href="#" th:href="@{/admin/blogs/{id}/delete(id=${blog.id})}" class="ui mini orange button">Delete</a>
                    </td>
                </tr>
                </tbody>
                <tfoot>
                <tr>
                    <th colspan="7">
                        <div class="ui pagination menu" th:if="${page.totalPages} > 1">
                            <a onclick="page(this)" th:attr="data-page=${page.number}-1" class="icon item" th:unless="${page.first}">
                                <i class="left chevron icon"></i>
                            </a>
                            <a onclick="page(this)" th:attr="data-page=${page.number}+1" class="icon item" th:unless="${page.last}">
                                <i class="right chevron icon"></i>
                            </a>
                        </div>
                        <a href="#" th:href="@{/admin/blogs/input}" class="ui right floated basic olive button">Add</a>
                    </th>
                </tr>
                </tfoot>
            </table>
        </div>
    </div>
</div>

<!-- Footer -->
<main class="m-sticky-site-content"></main>
<footer th:replace="admin/_fragments :: footer"></footer>
<!-- JavaScript -->
<th:block th:replace="_fragments :: script"></th:block>
<script>
    function page(obj) {
        $("[name='page']").val($(obj).data("page"));
        loaddata();
    }

    $("#search-btn").click(function () {
        $("[name='page']").val(0);
        loaddata();
    });

    function loaddata() {
        $("#table-container").load(/*[[@{/admin/blogs/search}]]*/"/admin/blogs/search",{
            title : $("[name='title']").val(),
            typeId : $("[name='typeId']").val(),
            recommend : $("[name='recommend']").prop('checked'),
            page : $("[name='page']").val()
        });
    }

    //close notification
    $('.message .close')
        .on('click', function () {
            $(this)
                .closest('.message')
                .transition('fade');
        });
</script>

</body>
</html>